'''
Created on Sep 1, 2013

@author: Blaine Buxton
'''

from itertools import imap
import math
import sys

class Turtle(object):
    def __init__(self, size):
        self.canvas = [['.' for _ in xrange(size)] for _ in xrange(size)]
        center = size / 2
        self.x = self.y = center
        self.curr_deg = 0
        self.mark()
        
    def run(self, commands):
        for each_command in commands:
            getattr(self,each_command.lower())(commands)
            
    def fd(self, rest):
        self.move_forward(rest, self.curr_deg)
            
    def bk(self, rest):
        self.move_forward(rest, self.curr_deg + 180)
            
    def move_forward(self, rest, direction):
        steps = int(rest.next())
        for _ in xrange(steps):
            self.move(direction)
            
    def rt(self, rest):
        self.rotate(rest, 1)
        
    def lt(self, rest):
        self.rotate(rest, -1)
            
    def rotate(self, rest, correction):
        angle = int(rest.next())
        self.curr_deg = self.grow_by(self.curr_deg, angle * correction, 360)
        
    def repeat(self, rest):
        times = int(rest.next())
        commands = rest.next()
        for _ in xrange(times):
            self.run(iter(commands))
            
    def mark(self):
        self.canvas[self.y][self.x] = 'X'
        
    def move(self, deg):
        x = self.amount_to_move(math.sin, deg)
        y = -1 * self.amount_to_move(math.cos, deg)
        size = len(self.canvas)
        self.x = self.grow_by(self.x, x, size)
        self.y = self.grow_by(self.y, y, size)
        self.mark()
        
    def grow_by(self, value, inc, max_value):
        result = (value + inc) % max_value
        while (result < 0):
            result = result + max_value
        return result
        
    def amount_to_move(self, tri_func, deg):
        return int(round(tri_func(math.radians(deg))))
        
    def __str__(self):
        return '\n'.join([''.join(each) for each in self.canvas])

def tokenize(io_input):
    for line in imap(lambda raw: raw.strip(), io_input):
        if line:
            for token in line.split():
                if token:
                    yield token
                    
def parse(io_input):
    def go(answer,tokens):
        for token in tokens:
            if '[' == token:
                answer.append(go([], tokens))
                continue
            if ']' == token:
                return answer
            answer.append(token)
        return answer
    return go([],tokenize(io_input))

def execute(io_input):
    temp = parse(io_input)
    commands = iter(temp)
    size = int(commands.next())
    turtle = Turtle(size)
    turtle.run(commands)
    return turtle

if __name__ == "__main__":
    for arg in sys.argv[1:]:
        with open(arg,'r') as io_input:
            print "Executing: " + arg
            turtle = execute(io_input)
            print turtle
